package com.gy.spark.core.others.topn

import org.apache.spark.{SparkContext, SparkConf}

object GroupTopN {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local")
    val sc = new SparkContext(conf)

    val lines = sc.textFile("spark/input/scores.txt")

    val lineList = lines.map(x => (x.split("\t")(0), x.split("\t")(1))).groupByKey()

    lineList.foreach(println)

    println("*****************************")

    val topList = lineList.map(x => {
      var t = List[Int]()
      for (a <- x._2) {
        t = t.::(a.toInt)
      }
      println(x._1)
      t.sortBy { x => -x }.take(3)
    })
    topList.foreach {
      println
    }
  }
}